from datetime import datetime


def classifyMemory(result_data):
    """
    把记忆碎片按年月日分类
    返回格式如下:
            {'year': { 'month': { 'day': [ memory_id, memory_id, ... ] }}}
    simple: {'2020': {'12': {'13': [2], '12': [3]}, '11': {'12': [4, 5]}}, '2019': {'11': {'12': [6]}}, '2018': {'11': {'12': [7]}}}

    该样列含义: 2020年有12月和11月有记忆，12月中13，12日有记忆，13日对应的记忆编号为2，12日对应的记忆编号为3
              2019年分析同上

    """
    # 算法思想: 先按照年份分，在此基础上按月分，再按日分

    classified_data = {} # 分类后的数据存储
    for memory_item in result_data:
        # 分类年份
        create_time_year, create_time_month, create_time_day = decomposeDateTime(memory_item)

        classified_data.update( { create_time_year:{} })

    for memory_item in result_data:
        # 分类年月
        create_time_year, create_time_month, create_time_day = decomposeDateTime(memory_item)

        if  create_time_year in  classified_data.keys():
            classified_data[create_time_year].update( { create_time_month:{} } )

    for memory_item in result_data:
        # 分类年月日
        create_time_year, create_time_month, create_time_day = decomposeDateTime(memory_item)

        if  create_time_month in  classified_data[create_time_year].keys():
            classified_data[create_time_year][create_time_month].update( { create_time_day:[] } )

    for memory_item in result_data:
        # 分类年月日，以及这一天中的 memory_id
        create_time_year, create_time_month, create_time_day = decomposeDateTime(memory_item)

        if  create_time_day in classified_data[create_time_year][create_time_month].keys():
            classified_data[create_time_year][create_time_month][create_time_day].append(memory_item['memory_id'])

    return classified_data

def decomposeDateTime(memory_item):
    """
    分解日期
    :param memory_item:
    :return create_time_year,create_time_month,create_time_day:
    """
    create_time = datetime.strptime(memory_item['create_time'], "%Y-%m-%d")
    create_time_year = str(create_time.year)
    create_time_month = str(create_time.month)
    create_time_day = str(create_time.day)

    return create_time_year,create_time_month,create_time_day


if __name__ == '__main__':
    # 测试数据
    result_data = [{'belonging_user': '3180312023', 'memory_id': 2, 'memory_title': '测试2',
                    'memory_content': '\n                                    <div id="myEditor" style="width:730px;height:400px;" name="myEditor">测试2</div>\n                                <p><br></p>',
                    'memory_level': 1, 'is_modify': 1, 'status': 0, 'end_time': '2020-12-13 23:59:59',
                    'create_time': '2020-12-13 09:18:02'},
                   {'belonging_user': '3180312023', 'memory_id': 3, 'memory_title': '测试1',
                    'memory_content': '\n                                    <div id="myEditor" style="width:730px;height:400px;" name="myEditor">测试1</div>\n                                <p><br></p>',
                    'memory_level': 1, 'is_modify': 1, 'status': 0, 'end_time': '2020-12-13 23:59:59',
                    'create_time': '2020-12-12 08:48:38'},
                   {'belonging_user': '3180312023', 'memory_id': 4, 'memory_title': '测试1',
                    'memory_content': '\n                                    <div id="myEditor" style="width:730px;height:400px;" name="myEditor">测试1</div>\n                                <p><br></p>',
                    'memory_level': 1, 'is_modify': 1, 'status': 0, 'end_time': '2020-12-13 23:59:59',
                    'create_time': '2020-11-12 08:48:38'},
                   {'belonging_user': '3180312023', 'memory_id': 5, 'memory_title': '测试1',
                    'memory_content': '\n                                    <div id="myEditor" style="width:730px;height:400px;" name="myEditor">测试1</div>\n                                <p><br></p>',
                    'memory_level': 1, 'is_modify': 1, 'status': 0, 'end_time': '2020-12-13 23:59:59',
                    'create_time': '2020-11-12 08:48:38'},
                   {'belonging_user': '3180312023', 'memory_id': 6, 'memory_title': '测试1',
                    'memory_content': '\n                                    <div id="myEditor" style="width:730px;height:400px;" name="myEditor">测试1</div>\n                                <p><br></p>',
                    'memory_level': 1, 'is_modify': 1, 'status': 0, 'end_time': '2020-12-13 23:59:59',
                    'create_time': '2019-11-12 08:48:38'},
                   {'belonging_user': '3180312023', 'memory_id': 7, 'memory_title': '测试1',
                    'memory_content': '\n                                    <div id="myEditor" style="width:730px;height:400px;" name="myEditor">测试1</div>\n                                <p><br></p>',
                    'memory_level': 1, 'is_modify': 1, 'status': 0, 'end_time': '2020-12-13 23:59:59',
                    'create_time': '2018-11-12 08:48:38'}, ]

    classified_data = classifyMemory(result_data)
    print(classified_data)
    # {'2020': {'12': {'13': [2], '12': [3]}, '11': {'12': [4, 5]}}, '2019': {'11': {'12': [6]}}, '2018': {'11': {'12': [7]}}}
